package com.itheima.kafka.boot.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.kafka.boot.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    public static final String TOPIC = "itcast-heima-boot";


    @Autowired
    KafkaTemplate kafkaTemplate;


    @GetMapping("hello")
    public String hello(String msg) {
        kafkaTemplate.send(TOPIC, "Hello World Kafka boot" + msg);

        return "OK";
    }

    @GetMapping("send")
    public String sendUser() {

        User user = new User();
        user.setAge(20);
        user.setUsername("zs");

        // JSON
        String userJson = JSON.toJSONString(user);

        kafkaTemplate.send(TOPIC, userJson);

        return "OK";
    }
}